﻿using System;
using System.Collections.Generic;
using System.Linq;
using Framework.Poco;
using Shell.DomainDot;

namespace Framework.Domain
{
    /// <summary>
    /// 全局上下文
    /// </summary>
    public class GlobalContext : DomainDotBase
    {
        /// <summary>
        /// 获取或设置所有经销商分组
        /// </summary>
        public WithAll<AgencyGroup> AgencyGroups { private get; set; }

        /// <summary>
        /// 获取或设置所有市区
        /// </summary>
        public WithAll<District> Districts { private get; set; }

        /// <summary>
        /// 获取或设置所有经销商
        /// </summary>
        public WithAll<Agency> Agencies { private get; set; }

        /// <summary>
        /// 获取或设置所有特色婚宴
        /// </summary>
        public WithAll<FeaturedWedding> FeaturedWeddinies { private get; set; }

        /// <summary>
        /// 获取或设置所有教堂婚礼
        /// </summary>
        public WithAll<ChurchWedding> ChurchWeddings { private get; set; }

        /// <summary>
        /// 获取或设置所有消费类型
        /// </summary>
        public WithAll<ConsumptionClass> ConsumptionClass { private get; set; }

        /// <summary>
        /// 获取或设置所有服务费
        /// </summary>
        public WithAll<ServiceCost> ServiceCosts { private get; set; }

        /// <summary>
        /// 获取或设置经销商的上下文
        /// </summary>
        public WithAll<AgencyContext, Agency> AgencyContext { private get; set; }

        /// <summary>
        /// 获取所有特色婚宴
        /// </summary>
        public IEnumerable<FeaturedWedding> GetAllFeaturedWeddings()
        {
            return this.FeaturedWeddinies;
        }

        /// <summary>
        /// 获取所有教堂婚礼
        /// </summary>
        public IEnumerable<ChurchWedding> GetAllChurchWeddings()
        {
            return this.ChurchWeddings;
        }

        /// <summary>
        /// 获取所有消费类型
        /// </summary>
        public IEnumerable<ConsumptionClass> GetAllConsumptionClass()
        {
            return this.ConsumptionClass;
        }

        /// <summary>
        /// 获取所有服务费
        /// </summary>
        public IEnumerable<ServiceCost> GetAllServiceCosts()
        {
            return this.ServiceCosts;
        }

        /// <summary>
        /// 获取所有经销商分组
        /// </summary>
        public IEnumerable<AgencyGroup> GetAllAgencyGroups()
        {
            return this.AgencyGroups;
        }

        /// <summary>
        /// 获取所有市区
        /// </summary>
        public IEnumerable<District> GetAllDistricts()
        {
            return this.Districts;
        }

        /// <summary>
        /// 获取所有经销商
        /// </summary>
        public IEnumerable<Agency> GetAllAgencies()
        {
            return this.Agencies;
        }

        /// <summary>
        /// 经销商注册
        /// </summary>
        public void AgencyRegirter(Agency agency, Contact contact, Guid districtIdentity, Guid agencyGroupIdentity)
        {
            if (agency != null && contact != null)
            {
                agency.Password = EncryptionHelper.Md5Hash(agency.Password);
                agency.Created = DateTime.Now;
                var instance = this.AgencyContext.Create(agency);
                instance.BindDistrict(districtIdentity);
                instance.BindAgencyGroup(agencyGroupIdentity);
                instance.CreateContact(contact);
            }
        }

        /// <summary>
        /// 经销商修改
        /// </summary>
        public void EditAgency(Agency agency, Contact contact)
        {
            if (agency != null && contact != null)
            {
                var original = this.FindIdentityAgency(agency.Id);
                original.Address = agency.Address;
                original.Description = agency.Description;
                original.Website = agency.Website;
                this.Agencies.Submit();
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        public void ChangePassword(Agency agency, string hashedNewPassword)
        {
            if (agency != null && !String.IsNullOrEmpty(hashedNewPassword))
            {
                var original = this.FindIdentityAgency(agency.Id);
                original.Password = hashedNewPassword;
                this.Agencies.Submit();
            }
        }

        /// <summary>
        /// 经销商分组创建
        /// </summary>
        public void CreateAgencyGroup(AgencyGroup instance)
        {
            var entity = this.AgencyGroups.Create();
            PocoUtils.AssignProperty<AgencyGroup>(instance, entity, typeof(Guid));
            this.AgencyGroups.Submit();
        }

        /// <summary>
        /// 根据一个Guid找出一个经销商
        /// </summary>
        public Agency FindIdentityAgency(Guid identity)
        {
            return this.Agencies.SingleOrDefault(a => a.Id.Equals(identity));
        }

        /// <summary>
        /// 根据一个名称找出这个经销商
        /// </summary>
        public Agency FindNameAgency(string name)
        {
            var agency = this.Agencies.SingleOrDefault(a => a.Name == name);
            if (agency != null)
            {
                return agency;
            }
            return null;
        }

        /// <summary>
        /// 根据一个名称查找到一个经销商分组
        /// </summary>
        public AgencyGroup FindAgencyGroup(string name)
        {
            var agencyGroup = this.GetAllAgencyGroups().SingleOrDefault(ag => ag.Name.Equals(name));
            if (agencyGroup != null)
            {
                return agencyGroup;
            }
            return null;
        }

        /// <summary>
        /// 具有相同经销商分组名称的验证
        /// </summary>
        public bool HasSameAgencyGroup(string name)
        {
            var agencyGroups = this.GetAllAgencyGroups();
            var original = agencyGroups.SingleOrDefault(o => o.Name.Equals(name));
            if (original != null)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 具有相同经销商用户名称的验证
        /// </summary>
        public bool HasSameAgency(string name)
        {
            var agencies = this.GetAllAgencies();
            var original = agencies.SingleOrDefault(o => o.Name.Equals(name));
            if (original != null)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 具有相同经销商名称的验证
        /// </summary>
        public bool HasSameDisplayName(string name)
        {
            var agencies = this.GetAllAgencies();
            var original = agencies.SingleOrDefault(o => o.DisplayName.Equals(name));
            if (original != null)
            {
                return true;
            }
            return false;
        }
    }
}
